﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.Entity;
using System.Web;
using FisheryPlatform.Common.StaticCache;
using FisheryPlatform.Bussiness.Cache;

namespace FisheryPlatform.Bussiness.ShareCode
{
    public class DataDictionary
    {
        #region 单态控制
        private static DataDictionary _Instance = null;
        private static readonly object locker = new object();
        public static DataDictionary Instance
        {
            #region
            get
            {
                if (_Instance == null)
                {
                    lock (locker)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new DataDictionary();
                        }
                    }
                }
                return _Instance;
            }
            #endregion
        }
        #endregion

        public List<ShareCodeEntity> GetChildShareCodeList(DataClassEnum classEnum)
        {
            #region
            var classId = (long)classEnum;
            var cacheKey = string.Format(CacheKeyConstants.DataDictionaryFormatConstant, classId);
            var result = new List<ShareCodeEntity>();
            if (!CacheDataUtility.Intance.TryGet<List<ShareCodeEntity>>(cacheKey, out result))
            {
                result = ShareCodeManager.GetShareCodeListByClassId(classId);
                if (result.Count > 0)
                {
                    CacheDataUtility.Intance.Set(cacheKey, result);
                    //HttpRuntime.Cache.Add(cacheKey, result, null, System.Web.Caching.Cache.NoAbsoluteExpiration
                    //    , System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
                }
            }
            return result;
            #endregion
        }

        /// <summary>
        /// 获取指定类别 的父类的子类
        /// </summary>
        /// <param name="classEnum"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<ShareCodeEntity> GetChildShareCodeList(DataClassEnum classEnum, long parentId)
        {
            #region
            var classId = (long)classEnum;
            var result = new List<ShareCodeEntity>();
            var list = GetChildShareCodeList(classEnum);
            list.ForEach(item =>
            {
                if (item.ParentId == parentId)
                {
                    result.Add(item);
                }
            });
            return result;
            #endregion
        }

        /// <summary>
        /// 鱼品种列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<ShareCodeEntity> GetFishList(long parentId)
        {
            return GetChildShareCodeList(DataClassEnum.Fish, parentId);
        }

        /// <summary>
        /// 地区列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<ShareCodeEntity> GetLocationList(long parentId)
        {
            return GetChildShareCodeList(DataClassEnum.Fish, parentId);
        }

        /// <summary>
        /// 获取ShareCode实体
        /// </summary>
        /// <param name="shareCodeId"></param>
        /// <param name="dataClass"></param>
        /// <returns></returns>
        public ShareCodeEntity GetShareCode(long shareCodeId, DataClassEnum dataClass)
        {
            #region
            ShareCodeEntity result = new ShareCodeEntity();
            var list = GetChildShareCodeList(dataClass);
            list.ForEach(item =>
            {
                if (item.CodeID == shareCodeId)
                {
                    result = item;
                }
            });
            return result;
            #endregion
        }
    }
}
